<?php
namespace plugin\ky_black\app\admin\model;

use support\Model;

class Product extends Model
{
    protected $table = 'ky_scenic_product';
    protected $primaryKey = 'product_id';
    protected $guarded = [];
    
    protected $casts = [
        'images' => 'array',
        'status' => 'integer',
        'sort' => 'integer',
        'price' => 'decimal:2',
        'black_card_price' => 'decimal:2',
        'original_price' => 'decimal:2',
        'is_featured' => 'boolean',
        'min_people' => 'integer',
        'max_people' => 'integer',
        'validity_period' => 'integer'
    ];

    /**
     * 关联分类
     */
    public function category()
    {
        return $this->belongsTo(ProductCategory::class, 'category_id', 'category_id');
    }

    /**
     * 关联景区
     */
    public function scenicSpot()
    {
        return $this->belongsTo(BusinessPartner::class, 'scenic_spot_id', 'partner_id');
    }

    /**
     * 获取上架商品
     */
    public function scopeOnSale($query)
    {
        return $query->where('status', 1);
    }

    /**
     * 获取有库存商品
     */
    public function scopeInStock($query)
    {
        return $query->where('stock', '>', 0);
    }
} 